package com.iyao_app.adapter;

import android.graphics.Color;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.StoryDetail;
import com.iyao_app.entry.User;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.CountUtils;
import com.iyao_app.util.DbUtil;
import com.iyao_app.util.DistanceUtil;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.util.RxHeartLayout;
import com.iyao_app.util.TimeFormat;
import com.iyaowoqu.base_library.util.LogUtil;
import com.iyaowoqu.base_library.util.ScreenUtils;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/10.
 */

public class StoryWallAdapter extends BaseQuickAdapter<StoryDetail, StoryWallAdapter.ViewHolder> {


    LatLng myLatlng;
    float maxValue = ResourcesUtils.getDimension(R.dimen.y800);
    float minValue = ResourcesUtils.getDimension(R.dimen.y120);

    private int i1;

    public StoryWallAdapter(List<StoryDetail> data, LatLng myLatlng) {
        super(R.layout.item_story_wall, data);
        this.myLatlng = myLatlng;
    }


    @Override
    protected void convert(final ViewHolder helper, final StoryDetail item) {
        List<String> eventFileUrlList = item.eventFileUrlList;
        final ViewGroup.LayoutParams layoutParams = helper.mSdvPhoto.getLayoutParams();
        if (TextUtils.isEmpty(item.eventDesc)) {
            helper.mTvContent.setVisibility(View.GONE);
        } else {
            helper.mTvContent.setVisibility(View.VISIBLE);
            helper.mTvContent.setText(item.eventDesc);
        }
        helper.mTvTime.setText(TimeFormat.getTime(item.createTime));
        helper.mTvDistance.setText("距离" + DistanceUtil.getDistance(new LatLng(item.latitude, item.longitude), myLatlng));
        helper.mTvPinglun.setText(CountUtils.getCount(item.commentCount));
        String goodCount = item.goodCount;
        if (TextUtils.isEmpty(goodCount)) {
            helper.mTvXihuan.setText(String.valueOf("0"));
            item.count = 0;
        } else {
            helper.mTvXihuan.setText(CountUtils.getCount(goodCount));
            item.count = Integer.parseInt(goodCount);
        }


        if (item.goodVote) {
            helper.mIvLike.setImageResource(R.mipmap.btn_xihuan);
            helper.mTvXihuan.setTextColor(ResourcesUtils.getColor(R.color.text_color_rgb_153_code_999));
        } else {
            helper.mTvXihuan.setTextColor(ResourcesUtils.getColor(R.color.app_orange_ff773d));
            helper.mIvLike.setImageResource(R.mipmap.btn_wuxianxihuan);
        }
        final Random random = new Random();
        final int[] image = {R.mipmap.ic_a, R.mipmap.ic_b, R.mipmap.ic_c, R.mipmap.ic_d, R.mipmap.ic_e, R.mipmap.ic_f, R.mipmap.ic_g, R.mipmap.ic_h, R.mipmap.ic_i, R.mipmap.ic_j, R.mipmap.ic_k};

        LogUtil.e("====" + i1);
        helper.mLinXihuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                like(item, helper.mTvXihuan, helper.mIvLike);

                helper.heart_layout.post(new Runnable() {
                    @Override
                    public void run() {

                        i1 = random.nextInt(image.length);
                        int rgb = Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
                        helper.heart_layout.addHeart(rgb, image[i1], image[i1]);
                    }
                });
            }
        });
//        tvXihuan
        try {
            if (eventFileUrlList != null && eventFileUrlList.size() > 0) {
                String s = eventFileUrlList.get(0);
                if (item.height == 0) {
                    LogUtil.e("==layoutParams.height ==0==");
                    final int[] viewHeight = {layoutParams.height};
                    final int viewWith = (ScreenUtils.getScreenWidth() - 84) / 2;
                    ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                        @Override
                        public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                            if (imageInfo == null) {
                                return;
                            }
                            int height = imageInfo.getHeight();
                            int width = imageInfo.getWidth();
                            LogUtil.e("=======onFinalImageSet=========");
                            layoutParams.height = (int) ((float) (viewWith * height) / (float) width);
                            layoutParams.width = viewWith;
                            viewHeight[0] = layoutParams.height;
                            helper.mSdvPhoto.setLayoutParams(layoutParams);
                            item.width = viewWith;
                            item.height = layoutParams.height;
                            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) helper.heart_layout.getLayoutParams();
                            params.height = layoutParams.height + 150;
                            helper.heart_layout.setLayoutParams(params);
                        }

                        @Override
                        public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {

                        }

                        @Override
                        public void onFailure(String id, Throwable throwable) {
                            throwable.printStackTrace();
                        }
                    };

                    try {
                        Uri uri = Uri.parse(s);
                        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                                .setResizeOptions(new ResizeOptions(viewWith, viewHeight[0]))
                                .build();
                        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                                .setOldController(helper.mSdvPhoto.getController())
                                .setImageRequest(request)
                                .setControllerListener(controllerListener)
                                .build();
                        helper.mSdvPhoto.setController(controller);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {

                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) helper.heart_layout.getLayoutParams();
                    params.height = item.height + 150;
                    helper.heart_layout.setLayoutParams(params);
                    layoutParams.width = item.width;
                    layoutParams.height = item.height;
                    helper.mSdvPhoto.setLayoutParams(layoutParams);
                    Uri uri = Uri.parse(s);
                    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                            .setResizeOptions(new ResizeOptions(item.width, item.height))
                            .build();
                    PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                            .setOldController(helper.mSdvPhoto.getController())
                            .setImageRequest(request)
                            .build();
                    helper.mSdvPhoto.setController(controller);
                }
            } else {

            }
            String avatarUrl = item.avatarUrl;
            if (avatarUrl != null) {
                Uri uri = Uri.parse(avatarUrl);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(80, 80))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(helper.mSdvHead.getController())
                        .setImageRequest(request)
                        .build();
                helper.mSdvHead.setController(controller);
            } else {
                helper.mSdvHead.setImageResource(R.mipmap.ic_morentouxiang);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void like(final StoryDetail item, final TextView tv, final ImageView iv) {
        User user = DbUtil.getUser();
        NetUtils.getInstance().goodOrBad(item.id, user.id, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    item.count += 1;
                    item.goodCount = String.valueOf(item.count);
                    item.goodVote = false;
                    tv.setText(item.goodCount);
                    tv.setTextColor(ResourcesUtils.getColor(R.color.app_orange_ff773d));
                    iv.setImageResource(R.mipmap.btn_wuxianxihuan);
                }

            }

            @Override
            public void onFail(String message, int code) {

            }
        }, null);
    }


    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.sdv_photo)
        SimpleDraweeView mSdvPhoto;
        @BindView(R.id.tv_content)
        EmojiconTextView mTvContent;
        @BindView(R.id.sdv_head)
        SimpleDraweeView mSdvHead;
        @BindView(R.id.tv_distance)
        TextView mTvDistance;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.lin_1)
        LinearLayout mLin1;
        @BindView(R.id.iv_like)
        ImageView mIvLike;
        @BindView(R.id.tv_xihuan)
        TextView mTvXihuan;
        @BindView(R.id.lin_xihuan)
        LinearLayout mLinXihuan;
        @BindView(R.id.tv_pinglun)
        TextView mTvPinglun;
        @BindView(R.id.lin_pinlun)
        LinearLayout mLinPinlun;
        @BindView(R.id.heart_layout)
        RxHeartLayout heart_layout;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
